package cn.donting.jme.expansion.attribute.lmpl;

import lombok.Getter;

import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;


public class SelectAttribute<T> extends AbsLineAttribute<T, JComboBox> implements ItemListener {


    private T setValue;

    public SelectAttribute(String label) {
        super(new JComboBox<>());
        inputComponent.setRenderer(new ComboBoxRenderer());
        inputComponent.addItemListener(this::itemStateChanged);
    }

    @Override
    public void setValue(T value) {
        int itemCount = inputComponent.getItemCount();
        for (int i = 0; i < itemCount; i++) {
            Option<T> item = (Option) inputComponent.getItemAt(i);
            if (item.value.equals(value)) {
                inputComponent.setSelectedItem(item);
                return;
            }
        }
        setValue = value;
    }


    public void addOption(String label, T value, Icon icon) {
        Option<T> tOption = new Option<>(label, value, icon);
        addOption(tOption);
    }

    public void addOption(String label, T value) {
        Option<T> tOption = new Option<>(label, value);
        addOption(tOption);
    }

    public void addOption(Option<T> option) {
        inputComponent.addItem(option);
        if (setValue != null && setValue.equals(option.value)) {
            inputComponent.setSelectedItem(option);
            setValue = null;
        }
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
            Option<T> item = (Option) inputComponent.getSelectedItem();
            valueChange(item.value);
        }
    }

    @Override
    protected T getInputValue() {
        Option<T> selectedItem = (Option) inputComponent.getSelectedItem();
        if (selectedItem != null) {
            return selectedItem.value;
        }
        return null;
    }

    @Getter
    public static class Option<T> {
        public static final int ICON_SIZE = 16;
        private String label;
        private T value;
        private Icon icon;

        public Option(String label, T value) {
            this.label = label;
            this.value = value;
        }

        public Option(String label, T value, Icon icon) {
            this(label, value);
            this.icon = icon;
        }

        @Override
        public String toString() {
            return label;
        }
    }

    private static class ComboBoxRenderer extends BasicComboBoxRenderer {
        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof Option) {
                setIcon(((Option<?>) value).getIcon());
            }
            return this;
        }
    }
}
